package com.bite.springmvc.controller;

import com.bite.springmvc.model.UserInfo;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@RequestMapping("/request")
@RestController
public class RequestController {
    @RequestMapping("/param1")
    public String param1(String userName) {
        System.out.println("接收到参数："+ userName);
        return "接收到参数："+userName;
    }

    @RequestMapping("/param2")
    public String param2(Integer age) {
        return "2接收到参数:"+age;
    }

    @RequestMapping("/param3")
    public String param3(int age) {
        return "3接收到参数:"+age;
    }

    @Deprecated//废弃
    @RequestMapping("/param4")
    public String param4(String userName,String password) {
        return "接收到参数userName:"+userName+",password:"+password;
    }

    @RequestMapping("/param5")
    public String param5(UserInfo userInfo) {//这么多参数添加太麻烦，直接封装成对象
        return "接收到参数userInfo:"+userInfo;
    }

    @RequestMapping("/param6")//加个required参数就可以不填
    public String param6(@RequestParam(value = "userName",required = false) String name, String password) {
        return "接收到参数userName:"+name+",password:"+password;
    }

    @RequestMapping("/param7")
    public String param7(String[] arrParams) {
        return "接收到参数arrParams:"+ Arrays.toString(arrParams);
    }

    @RequestMapping("/param8")
    public String param8(@RequestParam("listParam") List<String> listParam) {
        return "接收到参数listParam:"+ listParam;
    }

    @RequestMapping("/param9")
    public String param9(@RequestBody UserInfo userInfo) {
        return "接收到参数userInfo:"+ userInfo;
    }

    @RequestMapping("/param10/{articleId}")
    public String param10(@PathVariable("articleId") Integer articleId) {
        return "接收到参数articleId:"+ articleId;
    }
    //多个参数(类似)重载
    @RequestMapping("/param10/{id}/{name}")
    public String param11(@PathVariable("id") Integer id,@PathVariable("name") String name) {
        return "接收到参数id:"+ id+",name:"+name;
    }
    //@RequestPart方法：将file赋值给files
    @RequestMapping("/param12")
    public String param12(@RequestPart("file") MultipartFile files) throws IOException {
//        System.out.println(files.getName());
        System.out.println(files.getOriginalFilename());
//        System.out.println(files.getContentType());
        files.transferTo(new File("C:/tmp/"+files.getOriginalFilename()));
        return "接收到文件"+files.getOriginalFilename();
    }

    @RequestMapping("getCookie")
    public String getCookie(HttpServletRequest request,HttpServletResponse response) {
        //Servlet的方法
        String userName = request.getParameter("userName");
//        String age = request.getParameter("age");
//        Integer age1 = Integer.parseInt(age);
        Cookie[] cookies = request.getCookies();
        System.out.println("获取Cookie信息");
        if(cookies!=null) {
            for(Cookie cookie:cookies) {
//                if("bite".equals(cookie.getName())) {
//                    //规范：前面尽量写常量，不然equals前面可能为空
//                }
                System.out.println(cookie.getName()+":"+cookie.getValue());
            }
        }
        return "获取Cookie成功";
    }

    @RequestMapping("/getCookie2")
    public String getCookie2(@CookieValue("bite") String bite) {
        System.out.println("从Cookie中获取bite:"+bite);
        return "获取Cookie成功";
    }

    @RequestMapping("/setSession")
    public String setSession(HttpServletRequest request) {
        //参数为true 如果session对象不存在，则创建一个session；如果存在，则直接返回
        HttpSession session = request.getSession(true);
        session.setAttribute("name","zhangsan");
        session.setAttribute("studentId","123456");
        return "设置session成功";
    }

    @RequestMapping("/getSession")
    public String getSession(HttpServletRequest request) {
        //参数为false 如果session对象不存在，则返回null
        HttpSession session = request.getSession(false);//从cookie中获取sessionid，根据sessionid获取对应的session
        if(session!=null) {
            System.out.println(session.getAttribute("name"));
            System.out.println(session.getAttribute("studentId"));
        }
        return "获取session成功";
    }

    @RequestMapping("/getSession2")
    public String getSession2(HttpSession session) {//内置对象，需要的话声明就加上，不需要就不加
        //session = 上面的request.getSession
        System.out.println("name:"+session.getAttribute("name"));
        System.out.println("studentId:"+session.getAttribute("studentId"));
        return "获取session成功";
    }

    @RequestMapping("/getSession3")
    public String getSession3(@SessionAttribute(value = "name",required = false) String name) {//默认必传参数
        System.out.println("name:"+name);
        return "获取session成功";
    }

    @RequestMapping("/getHeader")
    public String getHeader(HttpServletRequest request) {
        String userAgent = request.getHeader("User-Agent");
        return "userAgent:"+userAgent;
    }

    @RequestMapping("/getHeader2")
    public String getHeader2(@RequestHeader("User-Agent") String userAgent) {
        return "userAgent:"+userAgent;
    }
}
